package com.platform.service.impl;

import com.platform.controller.ReadExcel;
import com.platform.dao.MaterielDao;
import com.platform.entity.MaterielEntity;
import com.platform.entity.SysUserEntity;
import com.platform.service.MaterielService;
import com.platform.utils.RRException;
import com.platform.utils.ShiroUtils;
import com.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("materielService")
public class MaterielServiceImpl implements MaterielService {
    @Autowired
    private MaterielDao materielDao;

    @Override
    public List<MaterielEntity> queryList(Map<String, Object> map) {
        return materielDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return materielDao.queryTotal(map);
    }

    @Override
    public MaterielEntity queryObject(Integer id) {
        return materielDao.queryObject(id);
    }

    @Override
    public int delete(Integer id) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        MaterielEntity materielEntity = queryObject(id);
        materielEntity.setIsDelete(1);
        materielEntity.setIsOnSale(0);
        materielEntity.setUpdateUserId(user.getUserId());
        materielEntity.setUpdateTime(new Date());
        return materielDao.update(materielEntity);
        //return materielDao.delete(id);
    }

    @Override
    @Transactional
    public int back(Integer[] ids) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        int result = 0;
        for (Integer id : ids) {
            MaterielEntity materielEntity = queryObject(id);
            materielEntity.setIsDelete(0);
            materielEntity.setIsOnSale(1);
            materielEntity.setUpdateUserId(user.getUserId());
            materielEntity.setUpdateTime(new Date());
            result += materielDao.update(materielEntity);
        }
        return result;
    }

    @Override
    @Transactional
    public int deleteBatch(Integer[] ids) {
        int result = 0;
        for (Integer id : ids) {
            result += delete(id);
        }
        return result;
        //return materielDao.deleteBatch(ids);
    }

    @Override
    public int update(MaterielEntity materiel) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        materiel.setUpdateUserId(user.getUserId());
        materiel.setUpdateTime(new Date());
        return materielDao.update(materiel);
    }

    @Override
    @Transactional
    public int save(MaterielEntity materiel) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        Map<String, Object> map = new HashMap<>();
        map.put("materielCode", materiel.getMaterielCode());
        List<MaterielEntity> list = queryList(map);
        if (null != list && list.size() != 0) {
            throw new RRException("物料编码"+ materiel.getMaterielCode() +"已存在！");
        }
        materiel.setAddTime(new Date());
        materiel.setCreateUserId(user.getUserId());
        return materielDao.save(materiel);
    }

    @Override
    @Transactional
    //批量导入客户
    public boolean batchImport(String name,MultipartFile file){
        boolean b = false;
        //创建处理EXCEL
        ReadExcel readExcel=new ReadExcel();
        //解析excel，获取客户信息集合。
        List<MaterielEntity> materielList = readExcel.getExcelInfo(name ,file);
        if(materielList != null){
            b = true;
        }

        //迭代添加客户信息（注：实际上这里也可以直接将customerList集合作为参数，在Mybatis的相应映射文件中使用foreach标签进行批量添加。）
        /*for(Customer customer:customerList){
            customerDoImpl.addCustomers(customer);
        }*/
        return b;
    }
}
